// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import GameEvent from "../../Commons/GameEvent";
import { getZeroTime } from "../../GlobalFunction";
import ConfigManager from "../../Managers/ConfigManager";
import SaveManager from "../../Managers/SaveManager";
import { SigninEvent } from "../../Models/Signins/SigninManager";
import BaseUI from "../BaseUI";
import SigninItem from "../UIItems/SigninItem";

const { ccclass, property } = cc._decorator;

@ccclass
export default class SigninUI extends BaseUI {

    @property([SigninItem])
    itemArr: SigninItem[] = [];


    protected onEnable(): void {

        var userdata = SaveManager.single.userdata;
        let leftTime = cc.sys.now() - userdata.signinTime;
        if (leftTime >= 24 * 60 * 60 * 1000) {
            userdata.signinTime = getZeroTime();
            userdata.isSignin = false;
            userdata.signinDay++;
            if (userdata.signinDay > 6) {//签到7天满就重置
                userdata.signinDay = 0;
                for (const iterator of userdata.signinArr) {
                    iterator.isReceive = false;
                }
            }
            SaveManager.single.saveData();
        }
        this.drawView();
        GameEvent.eventNode.on(SigninEvent.SIGNIN_EVENT, this.onSignin, this);
    }
    protected onDisable(): void {
        GameEvent.eventNode.off(SigninEvent.SIGNIN_EVENT, this.onSignin, this);
    }
    onSignin() {
        this.drawView();
    }

    drawView() {
        var arr = ConfigManager.single.signinConfig;
        for (let i = 0; i < arr.length; i++) {
            this.itemArr[i].drawItem(i, arr[i]);
        }
    }
}
